package com.maxuxu.scihome.controller.Job;

import com.maxuxu.scihome.mapper.JobMapper;
import com.maxuxu.scihome.pojo.Job;
import com.maxuxu.scihome.pojo.Message;
import com.maxuxu.scihome.pojo.Pages;
import com.maxuxu.scihome.service.JobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @className: JobController
 * @author: Maxuxu
 * @date: 2022/7/4 22:38
 * @description: 工作的控制类
 */
@RestController
@Slf4j
public class JobController {
    @Autowired
    Job Job;
    @Autowired
    JobMapper jobMapper;
    @Autowired
    JobService jobService;
    @Autowired
    Message message;

    @PostMapping("/job/insert")
    public Message insert(HttpServletRequest request){
        String Jobname = request.getParameter("name");
        String remark = request.getParameter("remark");
        Job.setName(Jobname);
        Job.setRemark(remark);
        jobService.insertJob(Job);
        message.setJson("插入成功");
        return message;
    }

    @PostMapping("/job/select")
    public Pages findAll(long current, long size) {
        return jobService.selectPage(current, size);
    }


    @GetMapping("/job/removeDept")
    public Message removejob(HttpServletRequest request) {
        String ids = request.getParameter("ids").trim();
        String[] split = ids.split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String s:
                split) {
            Integer target = Integer.valueOf(s);
            list.add(target);
        }
        jobService.deletJob(list);
        message.setJson("删除成功"+ids);
        return message;
    }

    @GetMapping("/job/selectone")
    public Job findone(HttpServletRequest request){
        String name = request.getParameter("name");
        Job Job1 = new Job();
        Job1.setName(name);
        return jobService.selectByAny(Job1);
    }

    @PostMapping("/job/update")
    public Job update(String old,String name, String remark) {
        log.error("文档的旧标题是：{}，名字是:{},评论是：{}", old, name, remark);
        Job.setName(name);
        Job.setRemark(remark);
        jobService.updateJob(old, Job);
        return Job;
    }
}
